package com.robotserver.orders.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.common.DateUtil;
import com.robotserver.orders.entity.DyOrders;
import com.robotserver.orders.entity.DyTokens;
import com.robotserver.orders.mapper.DyOrdersMapper;
import com.robotserver.orders.mapper.DyTokensMapper;
import com.robotserver.orders.param.DyOrderSyncParam;
import com.robotserver.orders.param.biz.DyOrderPullParam;
import com.robotserver.orders.service.DyOrderCsjSyncService;
import com.robotserver.orders.service.DyOrderService;
import com.robotserver.orders.service.DyOrderSyncService;
import com.robotserver.orders.vo.DyOrderCsjTimeVO;
import com.robotserver.orders.vo.DyOrderTimeVO;
import com.robotserver.orders.vo.biz.PageVO;
import com.robotserver.orders.vo.biz.DyOrderVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: lucher
 * @Date: 2020/12/2 11:32
 * @Description:
 */
@Service
public class DyOrderServiceImpl implements DyOrderService {

    private static Logger logger = LoggerFactory.getLogger(DyOrderServiceImpl.class);
    @Autowired
    private DyOrderSyncService dyOrderSyncService;
    @Autowired
    private DyOrderCsjSyncService dyOrderCsjSyncService;
    @Autowired
    private DyTokensMapper dyTokensMapper;
    @Autowired
    private DyOrdersMapper dyOrdersMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public DyOrders getByOrderId(String orderId){
        return dyOrdersMapper.getByOrderId(orderId);
    }

    @Override
    public PageVO getOrderList(DyOrderPullParam param) {
        List<DyOrders> list = dyOrdersMapper.getOrderList(param);
        DyOrderVO orderVO = new DyOrderVO();
        orderVO.setRecords(list);
        return orderVO;
    }

    @Override
    public PageVO getOrderListNew(DyOrderPullParam param) {
        List<DyOrders> list = dyOrdersMapper.getOrderListNew(param);
        DyOrderVO orderVO = new DyOrderVO();
        orderVO.setRecords(list);
        return orderVO;
    }

    @Async("taskExecutor")
    @Override
    public void syncOrder_producer(DyOrderSyncParam dyOrderSyncParam) {
        try {
            //查询抖音token
            int now = DateUtil.getNowTime();
            //查询抖客联盟
            List<DyTokens> dyTokensList = dyTokensMapper.getAllTokens(now);
            //往redis队列中塞抖音tokens
            for (DyTokens dyTokens : dyTokensList) {
                dyTokens.setBeginTime(dyOrderSyncParam.getBegin_time());
                dyTokens.setEndTime(dyOrderSyncParam.getEnd_time());
                dyTokens.setType(dyOrderSyncParam.getDownTimeType());
                //对象转成JSON字符串，保存到redis队列中
                String dyTokensStr =  JSONObject.toJSONString(dyTokens);
                redisTemplate.opsForList().leftPush(dyOrderSyncParam.getRedisQueueKey(), dyTokensStr);
                Thread.sleep(0); //0毫秒调用一次
            }
            //查询穿山甲联盟
            List<DyTokens> dyTokensListByCsj = dyTokensMapper.getAllTokensByCjs();
            //往redis队列中塞抖音tokens
            for (DyTokens dyTokens_csj : dyTokensListByCsj) {
                dyTokens_csj.setBeginTime(dyOrderSyncParam.getBegin_time());
                dyTokens_csj.setEndTime(dyOrderSyncParam.getEnd_time());
                dyTokens_csj.setType(dyOrderSyncParam.getDownTimeType());
                //对象转成JSON字符串，保存到redis队列中
                String dyTokensStr =  JSONObject.toJSONString(dyTokens_csj);
                redisTemplate.opsForList().leftPush(dyOrderSyncParam.getRedisQueueKey(), dyTokensStr);
                Thread.sleep(0); //0毫秒调用一次
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Async("taskExecutor")
    @Override
    public void syncOrder_consumer(DyOrderSyncParam dyOrderSyncParam) {

        while (true) {
            try {
                //从redis队列中获取联盟
                Object dyTokensObject = redisTemplate.opsForList().rightPop(dyOrderSyncParam.getRedisQueueKey());
                if (dyTokensObject != null) {
                    DyTokens dyTokens = JSONObject.parseObject(dyTokensObject.toString(),DyTokens.class);
                    //抖客联盟
                    if(dyTokens.getTokenType() == null || dyTokens.getTokenType().intValue() == 0){
                        //封装分段时间查询参数
                        List<DyOrderTimeVO> dyOrderTimeVOS = DateUtil.getDyOrderTimeScope(dyTokens.getBeginTime(), dyTokens.getEndTime());
                        dyOrderSyncService.threadSyncOrder_consumer(dyOrderTimeVOS, dyTokens, dyTokens.getType());
                    }else if(dyTokens.getTokenType().intValue() == 1){ //穿山甲联盟
                        //封装分段时间查询参数
                        List<DyOrderCsjTimeVO> dyOrderCsjTimeVOS = DateUtil.getDyOrderCsjTimeScope(dyTokens.getBeginTime(), dyTokens.getEndTime());
                        dyOrderCsjSyncService.threadSyncOrder_consumer(dyOrderCsjTimeVOS, dyTokens, dyTokens.getType());
                    }

                    Thread.sleep(100); //100毫秒调用一次
                }else{
                    // 没有需要处理的数据了
                    logger.info("-------------------------抖音联盟队列已经消费完成---------------！");
                    break;
                }
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
    }


}
